﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.OleDb;

namespace TradeMarkSys
{
    public partial class FrmLogin : Form
    {
        private Form frmMain;
        public FrmLogin()
        {
            InitializeComponent();
        }
        public FrmLogin(Form main)
        {
            InitializeComponent();
            frmMain = main;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection conn = null;
                try
                {
                    string sql = string.Format(@"select * from TS_User where UserName = '{0}'", txtUserName.Text);
                    string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Windows.Forms.Application.StartupPath + "\\TradeMarkSys.mdb";
                    conn = new OleDbConnection(connstr);
                    OleDbCommand cmd = new OleDbCommand(sql, conn);
                    conn.Open();
                    OleDbDataAdapter da = new OleDbDataAdapter();
                    da.SelectCommand = cmd;
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        string oldpwd = ds.Tables[0].Rows[0]["PassWord"].ToString();
                        if (!txtPassWord.Text.Equals(oldpwd))
                        {
                            MessageBox.Show("密码不正确,请重试!");
                            return;
                        }
                        else
                        {
                            FrmMain.CurrentUser = new Model.User();
                            FrmMain.CurrentUser.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                            FrmMain.CurrentUser.PassWord = ds.Tables[0].Rows[0]["PassWord"].ToString();
                            frmMain.Show();
                            this.Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show("用户名不存在,请重试!");
                        return;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("对不起系统出现错误");
                }
                finally
                {
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            try
            {
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
        }
    }
}
